package com.yloveg.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yloveg.common.DateUtil;
import com.yloveg.dao.ShopAddressMapper;
import com.yloveg.dao.ShopCartMapper;
import com.yloveg.dao.ShopOrdersMapper;
import com.yloveg.entity.ShopCartExtend;
import com.yloveg.entity.ShopOrders;
import com.yloveg.service.ShopOrdersService;

@Service
public class ShopOrdersServiceImpl implements  ShopOrdersService {
	
    @Autowired
    private ShopOrdersMapper shopOrdersMapper;
    
    @Autowired
    private ShopCartMapper shopCartMapper;
    
    @Autowired
    private ShopAddressMapper shopAddressMapper;

	@Override
	public PageInfo<ShopOrders> queryOrdersPage(int page, int limit, ShopOrders record) {
		// TODO Auto-generated method stub
		PageHelper.startPage(page, limit);
		List<ShopOrders> shopList = shopOrdersMapper.queryShops(record);
		Map<String,Object> tempMap = new HashMap<String,Object>();
		for (ShopOrders shopOrders : shopList) {
			String cartIds = shopOrders.getCartId();
			String[] cartId = cartIds.split(","); 
			tempMap.put("ids", cartId);
			StringBuffer sb = new StringBuffer();
			List<ShopCartExtend> listShopCartExtend = shopCartMapper.selectExtendByTerm(tempMap);
			for(int i=0;i<listShopCartExtend.size();i++){
				sb.append(listShopCartExtend.get(i).getProductName());
				if(i<listShopCartExtend.size()-1){
					sb.append("+");
				}
			}
			shopOrders.setProductName(sb.toString());
			shopOrders.setListShopCartExtend(listShopCartExtend);
			if(shopOrders.getAddressId()!=null){
				shopOrders.setShopAddress(shopAddressMapper.selectByPrimaryKey(shopOrders.getAddressId()));
			}
			
		}
		PageInfo<ShopOrders>resultPage=new PageInfo<ShopOrders>(shopList);
		return resultPage;
	}

	@Override
	public int insertOrder(ShopOrders record) {
		// TODO Auto-generated method stub
		record.setCreateTime(new Date());
		record.setState(1);
		record.setOrderNum("ORDER"+DateUtil.transPath());
		return shopOrdersMapper.insert(record);
	}

	@Override
	public int updateByPrimaryKeySelective(ShopOrders record) {
		// TODO Auto-generated method stub
		record.setUpdateDate(new Date());
		return shopOrdersMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public ShopOrders selectByPrimaryKey(Long id) {
		// TODO Auto-generated method stub
		return shopOrdersMapper.selectByPrimaryKey(id);
	}

	@Override
	public Map<String, String> queryCount(ShopOrders record) {
		// TODO Auto-generated method stub
		return shopOrdersMapper.queryCount(record);
	}







 


}
